using System;
using System.Windows;
using System.Windows.Controls;

namespace Waaahsabi.Controls.TheGame
{
	public class MailCreationControl : Control
	{
		public event EventHandler SendMail;
		public event EventHandler Back;
		#region To DependencyProperty
		public static readonly DependencyProperty ToProperty =
		DependencyProperty.Register
			(
			"To",
			typeof(string),
			typeof(MailCreationControl),
			new PropertyMetadata("")
			);

		public string To
		{
			get { return (string)GetValue(ToProperty); }
			set { SetValue(ToProperty, value); }
		}
		#endregion

		#region CC DependencyProperty
		public static readonly DependencyProperty CCProperty =
		DependencyProperty.Register
			(
			"CC",
			typeof(string),
			typeof(MailCreationControl),
			new PropertyMetadata("")
			);

		public string CC
		{
			get { return (string)GetValue(CCProperty); }
			set { SetValue(CCProperty, value); }
		}
		#endregion

		#region BCC DependencyProperty
		public static readonly DependencyProperty BCCProperty =
		DependencyProperty.Register
			(
			"BCC",
			typeof(string),
			typeof(MailCreationControl),
			new PropertyMetadata("")
			);

		public string BCC
		{
			get { return (string)GetValue(BCCProperty); }
			set { SetValue(BCCProperty, value); }
		}
		#endregion

		#region Subject DependencyProperty
		public static readonly DependencyProperty SubjectProperty =
		DependencyProperty.Register
			(
			"Subject",
			typeof(string),
			typeof(MailCreationControl),
			new PropertyMetadata(null)
			);

		public string Subject
		{
			get { return (string)GetValue(SubjectProperty); }
			set { SetValue(SubjectProperty, value); }
		}
		#endregion

		#region Body DependencyProperty
		public static readonly DependencyProperty BodyProperty =
		DependencyProperty.Register
			(
			"Body",
			typeof(string),
			typeof(MailCreationControl),
			new PropertyMetadata(null)
			);

		public string Body
		{
			get { return (string)GetValue(BodyProperty); }
			set { SetValue(BodyProperty, value); }
		}
		#endregion

		private Button btnSend;
		public TextBox txtTo { get; private set; }
		public TextBox txtCC { get; private set; }
		public TextBox txtBCC { get; private set; }
		public TextBox txtSubject { get; private set; }
		public TextBox txtBody { get; private set; }
		private Button btnBack { get; set; }
		public MailCreationControl()
		{
			DefaultStyleKey = typeof (MailCreationControl);
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			txtTo = GetTemplateChild("txtTo") as TextBox;
			txtCC = GetTemplateChild("txtCC") as TextBox;
			txtBCC = GetTemplateChild("txtBCC") as TextBox;
			txtSubject = GetTemplateChild("txtSubject") as TextBox;
			txtBody = GetTemplateChild("txtBody") as TextBox;
			if(txtBody != null)
			{
				txtBody.SelectionStart = 0;
			}
			btnSend = GetTemplateChild("btnSend") as Button;
			if(btnSend != null)
			{
				btnSend.Click += delegate
				{
					if (SendMail != null)
						SendMail(this, null);
				};
			}
			btnBack = GetTemplateChild("btnBack") as Button;
			if(btnBack != null)
			{
				btnBack.Click += delegate
				{
					if (Back != null)
						Back(this, null);
				};
			}
		}

		public void Reset()
		{
			txtTo.Text = null;
			txtBody.Text = null;
			txtCC.Text = null;
			txtBCC.Text = null;
			txtSubject.Text = null;
		}
	}
}